<template>
	<view class="assign">
		<sendNews ref="sendNews" @confirm="confirmExamine" :status="examineStatus"></sendNews>
		<zhilin-picker v-model="show" :data="selectData" @change="onChange" @confirm="confirmSeletc" />
	</view>
</template>

<script>
/*
审批一系列的流程，封装的组件
包括指派，消息，以及审批
通过外部调用init()函数
*/
import sendNews from "../sendNews/sendNews.vue"
import zhilinPicker from '../zhilin-picker/zhilin-picker.vue';
import service from '../../common/service/new_file.js';
import apiData from '../../common/utils/apiData.js';
export default {
	name: 'assignCharacter',
	components: { zhilinPicker,sendNews},
	data() {
		return {
			show: false,
			title: '选择',
			selectData: [], //渲染选择器的数据
			assignUser: [], //获取的原始数据
			workGroupeOptions: [],
			searchOption: [],
			sendOpt:null,//需要传出的参数
			examineStatus:'',
		};
	},
	props: {
		billtype: '',
		formPk:'',//传入的表单id
	},
	watch: {},
	methods: {
		open() {
			this.show = true;
		},
		init(status,pk) {
			//初始化时当前操作状态,1是通过，2是驳回
			if(this.formPk==''){
				this.formPk = pk
			}
			this.examineStatus = status
			if(status==1){
				//通过走的逻辑
				this.getUserList();
				this.sendOpt = {pk:this.formPk,spr:[]} 
			}else{
				//驳回就直接呼出消息框
				this.openSendView()
				this.sendOpt = {pk:this.formPk,spr:[]}
			}
		},
		openSendView(){
			//打开发送消息的视图
			this.$refs.sendNews.show();
		},
		confirmExamine(value){
			//消息确认框
			this.sendOpt.news = value
			this.outConfirm(this.sendOpt)
		},
		confirmSeletc(userNames) {
			//选择选中人的pk，并重新组合数据
			let sprs = userNames.map(item => {
				return (item = {
					spr: this.assignUser.filter(el => item == el.name)[0].value
				});
			});
			this.sendOpt =  {pk:this.formPk,spr:sprs}
			this.openSendView()
			//标记指定人
		},
		finishExamine(){
			//审批完成的事件
			this.$emit("finishExamine")
		},
		outConfirm(opt) {
			let _this = this;
			if (this.examineStatus == 1) {
				let data = apiData.salaryExamineData(this.billtype, true, opt.pk, opt.news, opt.spr); //FN(billtype, isAdopt,pk)
				service.common(data, {
					success: function(res) {
						uni.showToast({
							title: '审批通过',
							duration: 1000
						});
						setTimeout(()=>{
							_this.finishExamine()
						},1500)
					},
					fail: function(err) {
						console.log(err);
					},
					all: function(all) {
						console.log(all);
					}
				});
			} else if (this.examineStatus == 2) {
				let data = apiData.salaryExamineData(this.billtype, false, opt.pk, opt.news, opt.spr);
				service.common(data, {
					success: function(res) {
						uni.showToast({
							title: '已驳回',
							icon: 'none',
							duration: 1500
						});
						setTimeout(()=>{
							_this.finishExamine()
						},1500)
					},
					fail: function(err) {
						console.log(err);
					}
				});
			}
		}, //审核意见弹出框
		
		getUserList() {
			//获取查询用户列表
			let billtype = this.billtype;
			let form_pk = this.formPk;
			let data = apiData.assign(form_pk, [], billtype, 'approve','N');
			service.common(data, {
				success: res => {
					if (JSON.stringify(res.data.VALUE) != '{}') {
						//获取可指派的用户并重新组合数据
						let newObj = JSON.parse(JSON.stringify(res.data.VALUE));
						let resArr = [];
						for (let key in newObj) {
							resArr.push({
								name: newObj[key],
								value: key
							});
						}
						this.assignUser = resArr;
						this.selectData = resArr.map(item => (item = item.name));
						
						uni.showModal({
							title: '提示',
							content: '检测到有可指派用户，是否进行指派？',
							success: mol => {
								if (mol.confirm) {
									//若确认指派则打开选择器
									this.open();
								} else if (mol.cancel) {
									//不指派的直接传出空数据列表
									this.openSendView();
								}
							}
						});
					} else {
						this.openSendView();
					}
				}
			});
		}
	},
	created() {}
};
</script>

<style lang="scss" scoped>
.user-wrap {
	width: 60vw;
	padding: 40rpx;
	border-radius: 16rpx;
	background: #7da4ff;
	overflow: hidden;
}
</style>
